#include"preCmpH.h"
#include "UIButton.h"
#include"UIFontManager.h"
#include"UIManager.h"

BEGIN_NAMESPACE(ui_manager)
BEGIN_NAMESPACE(ui_controls)

UIButton::UIButton(void) :
state_(0),
enabled_(true),
on_click_(null),
click_img_(),
hover_img_(),
text()
{
}
UIButton::~UIButton(void)
{
}
void UIButton::Render(UIPoint parentPosistion)
{
	int texture_id = 0;
	if( state_ == 0 ){
		mcInterface_.getUIRenderer().DrawImage(
		pos_.getX() + parentPosistion.getX(),pos_.getY() + parentPosistion.getY(),
		size_.getWidth(), size_.getHeight(),
		*background_);
	}
	if( state_ == 1 ){
		mcInterface_.getUIRenderer().DrawImage(
		pos_.getX() + parentPosistion.getX(),pos_.getY() + parentPosistion.getY(),
		size_.getWidth(), size_.getHeight(),
		*hover_img_);
	}
	if( state_ == 2 ){
		mcInterface_.getUIRenderer().DrawImage(
		pos_.getX() + parentPosistion.getX(),pos_.getY() + parentPosistion.getY(),
		size_.getWidth(), size_.getHeight(),
		*click_img_);
	}
	
}
void UIButton::HandleEvent(ui_events::UIEvent ui_event)
{
	switch (ui_event.getMsgFlag())
	{
		case ui_events::UIEvent::FLAG_KEYBOARD:
			break;
		case ui_events::UIEvent::FLAG_MOUSE:
			{
				UIPoint mouse_pos = ui_event.getMousePosition(true);
					if((mouse_pos.getX() > pos_.getX() && mouse_pos.getX() < pos_.getX() + size_.getWidth()) &&
					   (mouse_pos.getY() > pos_.getY() && mouse_pos.getY() < pos_.getY() + size_.getHeight())){
						   if(state_ == 2){
							   if(ui_event.getMouseButton() == ui_events::ui_input::BUTTON_LEFT &&
								   ui_event.getMouseButtonState() == ui_events::ui_input::ACTION_UP){
								   state_ = 1;
							   }
						   }
						   else {
							   state_ = 1;
							   if(ui_event.getMouseButton() == ui_events::ui_input::BUTTON_LEFT &&
								   ui_event.getMouseButtonState() == ui_events::ui_input::ACTION_DOWN){
								   setFocus(*this);
								   state_ = 2;
								   if(on_click_ != null){
										on_click_();
								   }
							   }
						   }
					} else {
						if(ui_event.getMouseButton() == 1 && ui_event.getMouseButtonState() == 2 ){
							   state_ = 1;
						}
						state_ = 0;
					}
			}
			break;
		case ui_events::UIEvent::FLAG_RENDER:
			{
				Render(ui_event.getRenderParentPosition());
				UIPoint aux = ui_event.getRenderParentPosition();
				ui_event.setRenderMsg(aux.getX() + pos_.getX(), aux.getY() + pos_.getY(), true);
				mcInterface_.getUIRenderer().setClipping(
					pos_.getX(),
					pos_.getY(),
					size_.getWidth(),
				size_.getHeight());
			}
			break;
		case ui_events::UIEvent::FLAG_TICK:
			{
				/*if(state_ == 0){
					state_ = 1;
				}else{
					state_ = 0;
				}*/
			}
			break;
		default:
			break;
	}
	text.HandleEvent(ui_event);
	mcInterface_.getUIRenderer().DisableClipping();
}
void UIButton::BoundBy( UIManager& manager, bool is_component )
{
	UIControl::BoundBy(manager,is_component);
	text.BoundBy(manager, true);
}

void UIButton::setSize( UISize size )
{
	size_ = size;
	text.setSize(size);
}

END_NAMESPACE // ui controls
END_NAMESPACE // ui manager
